package com.cnipr.patent;

import java.util.ArrayList;
import java.util.HashMap;

import com.chinasofti.framework.base.Utils;
import com.chinasofti.framework.dataaccess.Parameter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchResultActivity extends Activity 
	implements OnClickListener, OnItemClickListener
	{
	 
	public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setContentView(R.layout.search_result_layout);

		ImageView imgBack = (ImageView)findViewById(R.id.img_back);
		imgBack.setOnClickListener(this);
		ListView listPatent = (ListView)findViewById(R.id.list_patent);
		listPatent.setOnItemClickListener(this);

	   Intent intent = getIntent();
	   String conditionStr = intent.getExtras().getString("condition");
	   ArrayList<PatentSearchCondition> conditions =  new ArrayList<PatentSearchCondition>();
	   PatentSearchConditionParser parser = new PatentSearchConditionParser();
	   try {
		   parser.DoParse(conditionStr, conditions);
	   } 
	   catch (Exception e) {
		   e.printStackTrace();
	   }

	   ArrayList<Patent> patents = null;
	   PatentDataAccess dataAccess = new PatentDataAccess();
	   ArrayList<Parameter> params = new ArrayList<Parameter>();
	   for(int i = 0; i < conditions.size(); i++) {
		   PatentSearchCondition condition = conditions.get(i);
		   String name = condition.getString(PatentSearchCondition.FIELD_KEY);
		   String value = condition.getString(PatentSearchCondition.FIELD_VALUE);
		   value = value.replace("％", "%");
		   if(!value.trim().equals("")) {
			   Parameter param = new Parameter(name, String.class, value); 
			   params.add(param);
		   }
	   }
	   try {
		  patents = dataAccess.getPatents(params, 0, 20);
	   } 
	   catch (Exception e) {
		   e.printStackTrace();
	   }
	   
	   ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
	   for(int i = 0; i < patents.size(); i++) {
		   Patent patent = patents.get(i);
		   HashMap<String, Object> map = new HashMap<String, Object>();
		   map.put(Patent.FIELD_NAME, patent.get(Patent.FIELD_NAME));

		   map.put(Patent.FIELD_AN, patent.get(Patent.FIELD_AN));
		   map.put(Patent.FIELD_PD, Utils.getSimpleString(patent.get(Patent.FIELD_PD)));
		   map.put(Patent.FIELD_PIC, patent.get(Patent.FIELD_PIC));
		   map.put(Patent.FIELD_PA, patent.get(Patent.FIELD_PA));
		   map.put(Patent.FIELD_PNM, patent.get(Patent.FIELD_PNM));

		   map.put(Patent.FIELD_GPA, patent.get(Patent.FIELD_GPA));
		   listItem.add(map);
	   }

	   String[] from = {
			   Patent.FIELD_NAME,
			   Patent.FIELD_AN,
			   Patent.FIELD_PD,
			   Patent.FIELD_PIC,
			   Patent.FIELD_PA,
			   Patent.FIELD_PNM,
			   //Patent.FIELD_GPA
	   };
	   
	   int[] to = {
			   R.id.name,
			   R.id.an,
			   R.id.pd,
			   R.id.pic,
			   R.id.pa,
			   R.id.pnm,
			   //R.id.gpa
	   };
		   
	   SimpleAdapter adapter = new SimpleAdapter(
			   this, 
			   listItem, 
			   R.layout.patents_list_view, 
			   from,
			   to);
	   
	   listPatent.setAdapter(adapter);

	}

	@Override
	public void onClick(View arg0) {
		if(arg0.equals(findViewById(R.id.img_back))) {
			finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if(parent.equals(findViewById(R.id.list_patent))) {
			TextView tvAn = (TextView)view.findViewById(R.id.an);
			String an = tvAn.getText().toString();
			TextView tvPnm = (TextView)view.findViewById(R.id.an);
			String pnm = tvPnm.getText().toString();
			Intent intent = new Intent(this, PatentDetailActivity.class);
			intent.putExtra("an", an);
			intent.putExtra("pnm", pnm);
			startActivity(intent);
		}
	}

}
